// pages/doctor-programme-assess/doctor-programme-assess.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    diaryId: 1, //日记id
    pageSize: 10,
    pageNum: 1,
    arraydata: "", //下面赋的数据
    userdata: "", //下面赋的数据
    num: '',
    imgUrl: app.globalData.imgUrl,
    pinglun: "", //评论数据
    time: '', //转的时间错
    kong: '', //发布后清空
    isshow: 0, //默认点赞
    options: '', //默认
    usid: wx.getStorageSync('userid'),
    indexHeight: 'index9',
    isreply: false, // 是否为回复 false.直接发布 true.回复
    replyContent: '', // 回复内容
    replyPeople: '', // 回复的人(拼接后的) 格式:回复 ***:
    // replyid: '', // 评论指定id
    replyname: '', // 回复的人的昵称


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      schemeId: options.schemeid
    })
    this.commentPage(this.data.schemeId)
  },

  //获取日记评论
  commentPage: function () {
    app.request({
      url: 'doctor/demandApi/commentPage',
      data: {
        schemeId: this.data.schemeId, //日记id
        pageSize: this.data.pageSize, //每页条数
        pageNum: this.data.pageNum, //页数
      },
      success: (res) => {
        wx.hideLoading();
       
        res.data.forEach((item, idx) => {
          item.heightI = 'index' + idx
        })
        this.setData({
          pinglun: res.data,
          num: res.page.count
        })
      }
    })
  },



  // 内容
  save: function (e) {
    this.setData({
      replyContent: e.detail.value,
    })
  },

  // 回复指定人
  replyPeople: function (e) {
    console.log('回复指定人:', e);
    var commentid = e.currentTarget.dataset.commentid; // 回复的评论id
    var nickname = e.currentTarget.dataset.nickname; // 回复人的昵称
    let heightid = e.currentTarget.dataset.heightid;
    var replyPeople = '回复 ' + nickname + ':';
    this.setData({
      replyid: commentid,
      replyname: nickname,
      replyPeople: replyPeople,
      heightid: heightid
    })
  },

  // 处理回复
  bindReply(e) {
      var replyContent = this.data.replyContent; // 回复内容
      var replyPeople = this.data.replyPeople; // 回复的人 拼接后的
      if (!replyContent) {
        wx.showToast({
          title: '请输入内容',
          icon: 'none',
        })
        return false;
      }

    
      var isReply = replyContent.indexOf(replyPeople); // 是否为回复人或者为直接发布
    
      var replyId = ''; // 评论指定id
      if (!this.data.replyid) {
        console.log('普通回复2');
        replyId = ''
      } else {
        console.log('回复指定id1', this.data.replyid);
        replyId = this.data.replyid;

        replyContent = replyContent.replace(replyPeople, '');
      }


    
      var that = this;
      app.request({
        url: 'doctor/demandApi/comment',
        data: {
          schemeId: this.data.schemeId,
          content: replyContent,
          replyId: replyId,
        },
      
        success(res) {
        
            var pages = getCurrentPages();
            var prevPage = pages[pages.length - 2]; //上一个页面
            // let list = prevPage.data.infolist;
            let list = that.data.pinglun;
            list.forEach((item, idx) => {
              if (that.data.schemeId == item.schemeId) {
                prevPage.setData({
                  [`infolist[${idx}].commentCount`]: res.data.commentCount
                })
              }
            })

            let newindexHeight = 'index' + 0
            that.commentPage()
           
            that.setData({
              'pageNum': 1,
              'replyPeople': '',
              'replyid': '',
              'replyContent': '',
              indexHeight: that.data.replyid ? that.data.heightid : newindexHeight
            })

        
        }
      })


    
  },
  // 使页面滚动到底部
  pageScrollToBottom: function () {
    wx.createSelectorQuery().select('#j_page').boundingClientRect(function (rect) {
      console.log('底部：', rect);
      // 使页面滚动到底部
      wx.pageScrollTo({
        scrollTop: rect.height
      })
    }).exec();
  },


  touchBottom() {
    let { num, pinglun } = this.data;
    const that = this;
    app.showLoading();
    this.setData({
      pageSize: this.data.pageSize + 10
    })
    if (pinglun.length == num) {
      wx.showToast({
        title: '没有更多了哦~',
        icon: 'none',
        duration: 1000
      });
    } else {
      that.commentPage()
    }
  }
})